import 'package:flutter/material.dart';
import 'package:flutter_class2/common/global.dart';
import 'package:toast/toast.dart';

import 'account_controller.dart';

class ResetPWPage2 extends StatelessWidget {
  String idStr;
  String questionStr;
  String answerStr;
  ResetPWPage2({Key key, this.idStr, this.questionStr, this.answerStr}) : super( key: key);

  Widget build(BuildContext context){
    return ResetPWPage2State(idStr: idStr,questionStr: questionStr,answerStr: answerStr,);
  }
}

class ResetPWPage2State extends StatefulWidget {
  String idStr;
  String questionStr;
  String answerStr;
  ResetPWPage2State({Key key, this.idStr, this.questionStr, this.answerStr}) : super( key: key);

  @override
  _ResetPWPage2State createState() => new _ResetPWPage2State();
}

class _ResetPWPage2State extends State<ResetPWPage2State> {

  Global global = new Global();

  String inputAnswerStr = "";// 用户输入的答案
  var accountDatabaseHelper = new AccountDatabaseHelper();

  @override
  Widget build(BuildContext context) {

    final logo = Hero(// 创建hero动画，沟通两个组件之间的tag必须统一
      tag: 'headPic',
      child: CircleAvatar(
        backgroundColor: Colors.transparent,
        radius: 48.0,
        child: ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(90)),
          child: Image.asset('images/meng.png'),
        ),
      ),
    );

    final id = TextField(
      enabled: false,
      decoration: InputDecoration(
        hintText: "用户名： " + widget.idStr,
        contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
        border: global.diyColorTextFieldBorder(Colors.black),// 输入框的颜色
        enabledBorder: global.diyColorTextFieldBorder(Colors.black),// 输入框可编辑（但未选中）时的颜色
        focusedBorder: global.diyColorTextFieldBorder(Colors.blue),// 输入框获取焦点时的颜色
      ),
    );

    final question = TextField(
      keyboardType: TextInputType.text,
      enabled: false,
      autofocus: false,
      // initialValue: 'alucard@gmail.com',
      decoration: InputDecoration(
        hintText: '重置密码问题： ' + widget.questionStr,
        contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
        border: global.diyColorTextFieldBorder(Colors.black54),// 输入框的颜色
        enabledBorder: global.diyColorTextFieldBorder(Colors.black54),// 输入框可编辑（但未选中）时的颜色
        focusedBorder: global.diyColorTextFieldBorder(Colors.black54),// 输入框获取焦点时的颜色
      ),
    );

    final answer = TextField(
      keyboardType: TextInputType.text,
      autofocus: false,
      maxLines: 1,
      decoration: InputDecoration(
          hintText: '请输入你的密码重置答案',
          contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
        border: global.diyColorTextFieldBorder(Colors.black),// 输入框的颜色
        enabledBorder: global.diyColorTextFieldBorder(Colors.black54),// 输入框可编辑（但未选中）时的颜色
        focusedBorder: global.diyColorTextFieldBorder(Colors.black54),// 输入框获取焦点时的颜色
      ),
      onChanged: (value){
        inputAnswerStr = value;
      },
    );

    final loginButton = Hero(
        tag: 'loginButton',
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 16.0),
          child: RaisedButton(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(24),
            ),
            padding: EdgeInsets.all(12),
            color: Colors.blue,
            child: Text('确认重置', style: TextStyle(color: Colors.white)),
            onPressed: () async{
              if(inputAnswerStr == widget.answerStr){
                Account account = await accountDatabaseHelper.getAccount(widget.idStr);
                Account account2 = new Account(
                    account.id,
                    "123456",
                    account.phoneNum,
                    account.email,
                    account.nickName,
                    account.headPic,
                    account.sex,
                    account.age,
                    account.level,
                    account.resetPWQuestion,
                    account.resetPWAnswer);
                accountDatabaseHelper.updateAccount(account2);

                Toast.show("密码已重置为 123456", context);
              }else{
                Toast.show("答案有误，请重试", context);
              };
            },
          ),
        )
    );

    final tips = Text(
        '重置后密码将被设置为 123456',
        style: TextStyle(color: Colors.blue),
    );

    return Scaffold(
      backgroundColor: Colors.white,
      resizeToAvoidBottomInset: false,// 防止弹出输入法时内容溢出屏幕
      body: Center(
        child: Container(
          padding: EdgeInsets.only(left: 24.0, right: 24.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              logo,
              SizedBox(height: 48.0),
              id,
              SizedBox(height: 16.0),
              question,
              SizedBox(height: 12.0),
              answer,
              SizedBox(height: 12.0),
              tips,
              loginButton,
            ],
          ),
        )
      ),
    );
  }
}